Skip to content

End the chain span when a link raises, and nest parallel storage spans under it - #233

Merged
howethomas merged 2 commits into
mainfrom
howethomas/conserver-trace-context
Dec 3, 2026
Merged

howethomas merged 2 commits into
mainfrom
howethomas/conserver-trace-context

Conversation

@howethomas

@howethomas howethomas commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Fixes three tracing defects in conserver/main.py, seen in production traces.

A failed chain's span stayed open. process() entered the vcon_processing.<chain> span by hand and exited it only at the end of the success path. When a link raised, the span was never exited, so it stayed the current span in that worker thread. With CONSERVER_VCON_CONCURRENCY above 1, every later chain in the thread became its child: one trace collected several vCons' chains over many minutes, until the process restarted. The span ended only when garbage collection closed the abandoned context manager.

That span ended with status unset. Only the failing link.* span was ERROR, so a query on failed chains missed every vCon sent to the DLQ.

Parallel storage spans were roots. _process_storage_parallel runs each write in a thread pool, and pool threads start with an empty context, so each storage.* span began its own trace.

Change

  • process() now holds the span in a with block and runs the chain in _run_chain(), which is the old body unchanged. On failure the span records the exception, takes status ERROR, is detached and ended, and the exception reaches _handle_vcon, which moves the vCon to the DLQ as before. self._span is still set for the correlation attributes. The success path still sets OK.
  • Each parallel storage write runs in contextvars.copy_context(), so its span nests under the chain span.
  • Log lines, metrics and DLQ behavior are unchanged, apart from one debug line ("Failed to end span") that guarded the hand-rolled exit and is gone with it.

Tests

New conserver/tests/test_chain_span_context.py, with an in-memory span exporter and mocked Redis, queue, storage and links. It runs chains through _handle_vcon in one pool thread, as a worker does. Each test, with its result before the fix and after it:

  • test_failed_chain_detaches_and_ends_its_span_before_handle_vcon_returns: a chain whose link raises leaves no current span in the thread and has ended its chain span when _handle_vcon returns. Before: span 'vcon_processing.failing_chain' is still current in the worker thread. After: passes.
  • test_next_chain_in_the_same_thread_gets_a_root_span_in_a_new_trace: the next chain in that thread gets a root span in a new trace. Before: next chain span has parent 847c3cf9c1747c2f; expected a root span. After: passes.
  • test_failed_chain_span_is_error_with_the_exception_recorded: the failed chain span is ERROR with the exception recorded. Before: the failed chain span was never ended. After: passes.
  • test_interrupt_from_a_link_still_ends_and_detaches_the_chain_span_on_its_way_out[KeyboardInterrupt] and [SystemExit] (new): the interrupt from a link has ended and detached the chain span by the time it reaches the caller. Status is not checked. Before, on the old manual enter and exit: span 'vcon_processing.failing_chain' is still current in the worker thread, for both. After: both pass.
  • test_egress_context_is_taken_inside_the_chain_span (new): the context stored for an egress list carries the chain span's trace id and span id. Before: passes, since the old code already ran egress inside the span. On a copy that calls the wrap-up after the with block: Expected 'store_context_sync' to have been called once. Called 0 times. After: passes.
  • test_parallel_storage_spans_nest_under_the_chain_span: with parallel storage and two backends, each storage.* span has the chain span as parent and shares its trace. Changed: each fake write now waits on a two-party barrier, so both writes are in flight at once on two pool threads, and the test checks that. Without it the pool reused one thread for both. Before: storage.store_b is a root span. After: passes.
  • test_successful_chain_span_is_ok_with_correlation_attributes: the success path still ends OK with vcon.uuid, tenant.id and call.session_id on the chain span. Before: passes. After: passes.

Without the fix: 6 failed, 2 passed. With it: 8 passed. Full suite on the host with Python 3.12 and a local plain Redis: 808 passed, 17 skipped (the skips need API keys or migration URLs).

A backport to the production build branch follows in a separate PR.

🤖 Generated with Claude Code

…s under it.

A chain whose link raised never exited its vcon_processing span. The span
stayed attached in the worker thread, so every chain that thread ran next
became its child, and when garbage collection finally closed it, its status
was unset. process() now holds the span in a with block. On failure the span
records the exception, takes status ERROR, is detached and ended, and the
exception reaches _handle_vcon, which moves the vCon to the DLQ as before.

Parallel storage writes ran in pool threads, which start with an empty
context, so each storage span began its own trace. Each write now runs in a
copy of the caller's context and nests under the chain span.

conserver/tests/test_chain_span_context.py covers the failed chain, the next
chain in the same thread, the ERROR status, parallel storage, and the success
path with its correlation attributes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ool threads.

- The context stored for an egress list carries the chain span's trace and
  span ids. Moving the wrap-up outside the chain span makes it fail.
- A KeyboardInterrupt or SystemExit from a link has ended and detached the
  chain span by the time it reaches the caller. The old manual enter and
  exit fails it.
- The parallel storage test holds both writes in flight at once, so they
  run on two pool threads instead of one reused thread, and checks that.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@howethomas
howethomas merged commit 51c1d08 into main Dec 3, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant